# -*- coding: utf-8 -*-

'''
Created on 10.11.2013

@author: Christine
'''
import trunk.data
import os
import re
from io import open

def read_tags(tagfile):
    '''Reads in the tagfile, which is of the form:
    abbreviation (specification of abbreviation) {one or more corresponding POS tags} explanation
    Saves the abbreviation to a dictionary and maps them to the POS tags.
    
    @return dictionary of the form {abbreviation: [ POS tag 1, (POS tag 2, ...) ] }'''
    
    
    tagpattern = re.compile(ur"[\bA-Z/]+[\s[\bA-Z/]+]*", re.UNICODE) #pattern to find tags
    abbdict = dict() # a dictionary mapping the grammatical abbreviations found in the dictionary to tags
    
    with open(tagfile, encoding="utf8") as t:
        for line in t:
            #print("Zeile", line)
            if re.search(tagpattern, line):
                splits = re.split(tagpattern, line) #line split at tag
                abb = splits[0].rstrip()
                # print(abb)
                # print("Split", splits)
                tag = re.search(tagpattern, line).group().rstrip() # tag itself (e.g. 'NOM')
                #print("Tag", tag)
                tagsplit = tag.split()
                if len(tagsplit) > 1:
                    tag = tagsplit
                else:
                    tag = [tag] # abbreviation (e.g. 'nom.')
                
                
                abbdict[abb] = tag    
                
    return abbdict 
    #for (key, value) in abbdict.items():
    #   print (key, value)
def read_dictionary(dictfile, abbdict):  
    '''Function to read in the lemmas of the Uspanteko dictionary. Also determines a lemma's POS tag
    by mapping the grammatical class saved in the dictionary to a POS tag.
    
    @return Returns a dictionary with each lemma as a key.
    @version 2.0
    @param dictfile: file containing the Uspanteko dictionary'
    @param abbdict: dictionary containing the abbreviations for the tags found in the dictionary and 
    the corresponding tags'''
      
    lemmas = dict()
    lemmapattern = re.compile(ur"[A-ZÁÍÚÉÓ\']{2,}[\s[A-ZÁÍÚÉ\']{2,}]*", re.UNICODE)
    
    with open(dictfile, encoding = 'utf-8') as d:
        
        flag = False # the flag is used in case an abbreviation cannot be found in the same line as the lemma in the dictionary
        lemma = None
        for line in d:
            tag = ""
            if not flag: #if the abbreviation for a lemma could not be found in previous lines, check in the next lines for it and DON'T save a new lemma 
                lemma = re.findall(lemmapattern, line) # ATTENTION: Does this make sense? Might skip some actual lemmas.
            if lemma:  #continue only if a lemma has been found in this line or in a previous line (but for which an abbreviation could not be found)
                lemma = lemma[0]
                for abb in abbdict:
                    abbpattern = re.compile(ur"\s%s\s" % abb)
                    if re.findall(abbpattern, line): 
                        #print("Abk", abb, "Zeile", line)
                        tag = abbdict[abb]
                        flag = False
                        break
                    else:
                        flag = True
                    
            else:
                continue
            
            if flag:
                continue
            else:
                lemma = lemma.strip().lower().encode("utf-8") #eliminate trailing spaces at the right side, lower the word and encode it in utf-8
                for t in tag:
                    tag[tag.index(t)] = t.encode("utf-8")
                lemmas[lemma] = tag
                    
    return lemmas
            
if __name__ == '__main__':
    tags = read_tags(os.path.join(trunk.data.__path__[0], 'tags.txt'))
    lemmas = read_dictionary(os.path.join(trunk.data.__path__[0], 'dictionary.txt'), tags)
    i = 0

    """for (key, value) in lemmas.items():
        i += 1
    print(i)"""